package com.zhonghui.service.impl;

import com.zhonghui.common.core.utils.RedisUtils;
import com.zhonghui.common.core.result.FwResult;
import com.zhonghui.dto.AddressCreateDto;
import com.zhonghui.mapper.AddressMapper;
import com.zhonghui.model.Address;
import com.zhonghui.service.AddressService;
import com.zhonghui.vo.AddressListVo;
import com.zhonghui.vo.AddressRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author: FL
 * @create: 2024 2024/5/5 19:02
 */
@Service
@Slf4j
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public FwResult getAllAddress() {
        Long userId = Long.valueOf(redisUtils.get("userId"));
        List<Address> addressList = addressMapper.getAllAddress(userId);
        log.info("获取到的：addressList:{}",addressList);
        List<AddressRecordVo> addressRecordVoList = new ArrayList<>();
        for (Address address : addressList) {
            AddressRecordVo addressRecordVo = new AddressRecordVo();
            BeanUtils.copyProperties(address,addressRecordVo);
            addressRecordVoList.add(addressRecordVo);
        }
        AddressListVo addressListVo = AddressListVo.builder()
                .records(addressRecordVoList)
                .total(addressList.size())
                .build();
        return FwResult.ok(addressListVo);
    }

    @Override
    public FwResult createAddress(AddressCreateDto addressCreateDto) {
        Address address = new Address();
        BeanUtils.copyProperties(addressCreateDto,address);
//        随机生成地址id
        address.setAddressId(System.currentTimeMillis());
        log.info("address:{}",address);
        if (addressMapper.createAddress(address)==1){
            redisUtils.set(address.getAddressId().toString(),"address");
            return FwResult.ok("创建地址成功");
        }
        return FwResult.failedMsg("创建地址失败");
    }

    @Override
    public FwResult updateAddress(AddressCreateDto addressCreateDto, Long id) {
        Long userId = Long.valueOf(redisUtils.get("userId"));
        //如果id不存在，抛出404错误
        if (addressMapper.getAddressByAddressId(userId,id)==null){
            return FwResult.urlNotFoundMsg("地址不存在");
        }
        log.info("addressCreateDto:{}",addressCreateDto);
        if (addressMapper.updateAddress(addressCreateDto,id)==1){
            return FwResult.ok();
        }
        return FwResult.failedMsg("更新地址失败");
    }

    @Override
    public FwResult deleteAddress(Long id) {
        Long userId = Long.valueOf(redisUtils.get("userId"));
        if (addressMapper.getAddressByAddressId(userId,id)==null){
            return FwResult.urlNotFoundMsg("地址不存在");
        }
        if (addressMapper.deleteAddress(id)==1){
            return FwResult.ok();
        }
        return FwResult.failedMsg("其他错误");
    }

}
